package lagou.homework.lagou_homework_rocketmq.repository;

import lagou.homework.lagou_homework_rocketmq.entity.Goods;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface GoodsRepository extends JpaRepository<Goods,Integer> {

    @Query(nativeQuery = true,value = "update goods set stock = (stock+?2) where goodsId = ?1")
    @Modifying
    @Transactional
    public void increaseStock(Integer goodsId,int num);

    @Query(nativeQuery = true,value = "update goods set stock = (stock-?2) where goodsId = ?1")
    @Modifying
    @Transactional
    public void decreaseStock(Integer goodsId,int num);

    @Query(nativeQuery = true,value = "update goods set stock = ?2 where goodsId = ?1")
    @Modifying
    @Transactional
    public void updateStock(Integer goodsId,Integer stock);
}
